package com.gz.mylife.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.zl.smartmall.library.util.Tools;

public class PayDelivery extends BaseActivity implements OnClickListener {
	
	private TextView mOnlyWorkingDay;
	private TextView mOnlyWeekend;
	private TextView mAllDay;
	private TextView mCurrentDelivery;
//	private TextView mTvPay1;
//	private TextView mTvPay2;
	private TextView mCurrentPay;
	private Button mOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay_delivery);
		
//		mActionBar.setTitle("支付及配送");
		setActionbarTitle("支付方式选择", true);
		mOnlyWorkingDay = (TextView) findViewById(R.id.tv_only_working_day);
		mOnlyWorkingDay.setOnClickListener(this);
		mOnlyWeekend = (TextView) findViewById(R.id.tv_only_weekend);
		mOnlyWeekend.setOnClickListener(this);
		mAllDay = (TextView) findViewById(R.id.tv_all_day);
		mAllDay.setOnClickListener(this);
		mCurrentDelivery = mOnlyWorkingDay;
		mOk = (Button) findViewById(R.id.btn_ok);
		mOk.setOnClickListener(this);
		
		LinearLayout payMenuLayout = (LinearLayout) findViewById(R.id.layout_pay_menu);
		
		String[] payTypes = getIntent().getStringExtra("payType").split("_");
		String[] payNames = getIntent().getStringExtra("payName").split("_");
		for(int i = 0; i < payTypes.length; i++) {
			TextView v = (TextView) payMenuLayout.getChildAt(i);
			v.setId(R.id.tv_pay_option);
			v.setText(payNames[i]);
			v.setTag(Integer.valueOf(payTypes[i]));
			v.setOnClickListener(this);
			v.setVisibility(View.VISIBLE);
			if(i == 0) {
				v.setBackgroundResource(R.drawable.bg_mall_frame_selected);
				mCurrentPay = v;
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.pay_delivery, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok:
			Intent data = new Intent();
			data.putExtra("delivery", mCurrentDelivery.getText());
			data.putExtra("payType", (Integer) mCurrentPay.getTag());
			data.putExtra("payName", mCurrentPay.getText());
			setResult(Define.ACTIVITY_CODE_PAY_DELIVERY, data);
			finish();
			break;
			
//		case R.id.tv_pay_1:
//			if(!(Boolean) mTvPay1.getTag()) {
//				mTvPay1.setTag(true);
//				mTvPay1.setBackgroundResource(R.drawable.bg_mall_frame_selected);
//				mTvPay2.setTag(false);
//				mTvPay2.setBackgroundResource(R.drawable.bg_mall_frame_unselected);
//			}
//			break;
//			
//		case R.id.tv_pay_2:
//			if(!(Boolean) mTvPay2.getTag()) {
//				mTvPay2.setTag(true);
//				mTvPay2.setBackgroundResource(R.drawable.bg_mall_frame_selected);
//				mTvPay1.setTag(false);
//				mTvPay1.setBackgroundResource(R.drawable.bg_mall_frame_unselected);
//			}
//			break;
			
		case R.id.tv_pay_option:
			if(mCurrentPay != v) {
				mCurrentPay.setBackgroundResource(R.drawable.bg_mall_frame_unselected);
				v.setBackgroundResource(R.drawable.bg_mall_frame_selected);
				mCurrentPay = (TextView) v;
			}
			break;

		default:
			if(v == mCurrentDelivery) {
				return;
			}
			mCurrentDelivery.setBackgroundResource(R.drawable.bg_mall_frame_unselected);
			mCurrentDelivery.setPadding(0, Tools.dip2px(getApplicationContext(), 10), 
					0, Tools.dip2px(getApplicationContext(), 10));
			v.setBackgroundResource(R.drawable.bg_mall_frame_selected);
			v.setPadding(0, Tools.dip2px(getApplicationContext(), 10), 
					0, Tools.dip2px(getApplicationContext(), 10));
			mCurrentDelivery = (TextView) v;
			break;
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}
